package drawapp.cw.com.recyclerviewdemo;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

/**
 * Created by Ade-rui on 2016/12/19.
 */
public class BaseRecyclerAdapter extends RecyclerView.Adapter {

    public static final int STATE_LOAD_MORE = 1001;

    /**
     * 是否加载更多
     */
    private boolean isLoadMore;

    private List<String> datas;

    public BaseRecyclerAdapter(List<String> datas){
        this.datas = datas;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(viewType == STATE_LOAD_MORE){
            return onCreateFooterView(parent);
        }

        return onCreateNormalViewHolder(parent);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        //如果是最后一个Item并且要加载更多，则只显示加载更多的布局，不进行bind数据操作
        if(position == getItemCount() - 1 && isLoadMore){
            if(holder.itemView.getLayoutParams() instanceof StaggeredGridLayoutManager.LayoutParams){
                StaggeredGridLayoutManager.LayoutParams params = (StaggeredGridLayoutManager.LayoutParams) holder.itemView.getLayoutParams();
                params.setFullSpan(true);
            }
            return;
        }
        //这里其实应该写一个BaseViewHolder 调用其中的onBind抽象方法，让其具体的子类去绑定
        //不要用强转这种方式，这里为了做例子方便所以强转
        ((ContentViewHolder)holder).tvContent.setText(datas.get(position));
    }

    @Override
    public int getItemViewType(int position) {
        if(enableLoadMore(position)){
            return STATE_LOAD_MORE;
        }
        return super.getItemViewType(position);
    }

    /**
     * 检查是否显示加载更多布局
     * @param position
     * @return
     */
    public boolean enableLoadMore(int position){
        return position == getItemCount() - 1 && isLoadMore;
    }

    /**
     * 如果要加载更多，那么返回的count要比真实数据源的数量大于1，方便在这个多余的位置显示加载更多的布局
     * @return
     */
    @Override
    public int getItemCount() {
        return (isLoadMore?1:0) + datas.size();
    }

    /**
     * 创建加载更多的布局
     * @param parent
     * @return
     */
    public RecyclerView.ViewHolder onCreateFooterView(ViewGroup parent){
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_footer_load_more,parent,false);
        return new FooterView(view);
    }

    /**
     * 创建正常的内容布局
     * @param parent
     * @return
     */
    public RecyclerView.ViewHolder onCreateNormalViewHolder(ViewGroup parent){
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_content_view,parent,false);
        return new ContentViewHolder(view);
    }

    /**
     * 是否显示 加载更多的布局
     */
    public void loadMoreChangeState(boolean isLoadMore){
        this.isLoadMore = isLoadMore;
        if(isLoadMore){
            //在最后一个Item + 1的位置插入view,recyclerview自带的方法
            notifyItemInserted(getItemCount());
        }else {
            //移除最后一个Item + 1 的位置上的view,recyclerview自带的方法
            notifyItemRemoved(getItemCount());
        }
    }

    /**
     * 加载更多显示的布局
     */
    public class FooterView extends RecyclerView.ViewHolder{

        public FooterView(View itemView) {
            super(itemView);
        }
    }

    public void addAll(List<String> list){
        datas.addAll(list);
        notifyDataSetChanged();
    }

    public void clear(){
        datas.clear();
        notifyDataSetChanged();
    }

    /**
     * 内容ViewHolder
     */
    public class ContentViewHolder extends RecyclerView.ViewHolder{

        public TextView tvContent;

        public ContentViewHolder(View itemView) {
            super(itemView);
            tvContent = (TextView) itemView.findViewById(R.id.tv_content);
        }

    }


    /**
     * 构造函数中的spanCount是我们指定几列
     * @return
     */
    public  static  class  FooterSpanSize extends GridLayoutManager.SpanSizeLookup {

        private  BaseRecyclerAdapter adapter;
        private  GridLayoutManager layoutManager;

        public FooterSpanSize(BaseRecyclerAdapter adapter,GridLayoutManager layoutManager){
            this.layoutManager = layoutManager;
            this.adapter = adapter;
        }

        /**
         * 每显示一个Item的时候这个方法就会被回调，并传入item的索引position;
         * 为什么要持有Adapater的引用？ 因为我们需要让Adapter来判断是否是加载更多的布局
         * 为什么要持有自身LayoutManager的引用？ 因为我们需要知道总共几列
         * @param position
         * @return
         */
        @Override
        public int getSpanSize(int position) {
            if(adapter.enableLoadMore(position)){
                return layoutManager.getSpanCount();
            }
            return 1;
        }
    }

}
